package com.oxbix.spanlogistics.share.tencent;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Toast;

import com.kye.express.view.ToastManager;
import com.oxbix.spanlogistics.share.Contants;
import com.oxbix.spanlogistics.share.ShareBean;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.util.ArrayList;


/**
 * Created by chen on 18/1/10.
 */

public class TencentShareHelper {

    private Activity mAct;

    private static TencentShareHelper mInstance;
    //qq share
    private Tencent mTencent;
    //wechat share
    //微信分享图片 固定宽高 因为过高分享失败 过低分享图片模糊
    private static final int THUMB_WIDTH = 200;
    private static final int THUMB_HEIGHT = 320;
    public static String SHARE_FLAG_WECHAT = "微信好友";
    public static String SHARE_FLAG_WECHAT_MOMENTS = "微信朋友圈";
    private IWXAPI mWXApi;

    private TencentShareHelper() {

    }

    public static TencentShareHelper getInstace() {
        if (mInstance == null) {
            mInstance = new TencentShareHelper();
        }
        return mInstance;
    }

    public void init(Activity mAct) {
        this.mAct = mAct;
        if (mTencent == null) {
            mTencent = Tencent.createInstance(Contants.TENCENT_QQ_APPID, mAct.getApplicationContext());
        }
        if (mWXApi == null) {
            mWXApi = WXAPIFactory.createWXAPI(mAct, Contants.TENCENT_WECHAT_APPID);
            mWXApi.registerApp(Contants.TENCENT_WECHAT_APPID);
        }
    }


    public void shareQQ(ShareBean shareBean) {

        Bundle bundle = new Bundle();
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareBean.getLinkUrl());
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE, shareBean.getTitle());
//        bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareBean.getUrl());
        ArrayList<String> imgUrlList = new ArrayList<>();
        imgUrlList.add(shareBean.getUrl());
        bundle.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imgUrlList);// 图片地址
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareBean.getSummary());
        bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, "跨越速运");

        mTencent.shareToQQ(mAct, bundle, tencentRequestListener);
    }


    //分享到QZone
    public void shareToQZone(ShareBean shareBean) {
        Bundle params = new Bundle();
        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, shareBean.getTitle());// 标题
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, shareBean.getSummary());// 摘要
        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, shareBean.getLinkUrl());// 内容地址
        ArrayList<String> imgUrlList = new ArrayList<>();
        imgUrlList.add(shareBean.getUrl());
        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imgUrlList);// 图片地址
        // 分享操作要在主线程中完成
        // TODO Auto-generated method stub
        mTencent.shareToQzone(mAct, params, tencentRequestListener);
    }


    /**
     * 微信、朋友圈分享
     *
     * @param shareBean
     * @param type      0 微信分享 1朋友圈分享
     */
    public void shareToWechat(ShareBean shareBean, int type) {

        Bitmap bm = BitmapFactory.decodeFile(shareBean.getPath());
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = shareBean.getLinkUrl();
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = shareBean.getTitle();
        msg.description = shareBean.getSummary();
        //设置缩略图
        if (bm != null && !"".equals(bm)) {
            Bitmap thumb = Bitmap.createScaledBitmap(bm, THUMB_WIDTH, THUMB_HEIGHT, true);
            bm.recycle();
            msg.setThumbImage(thumb);
        }
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = type == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        mWXApi.sendReq(req);
    }


    public TencentRequestListener tencentRequestListener = new TencentRequestListener() {

        @Override
        public void onComplete(Object o) {
            super.onComplete(o);
            ToastManager.show(mAct, "分享成功！", Toast.LENGTH_SHORT);
        }

        @Override
        public void onError(UiError uiError) {
            super.onError(uiError);
            ToastManager.show(mAct, "分享异常！", Toast.LENGTH_SHORT);

        }

        @Override
        public void onCancel() {
            super.onCancel();
            ToastManager.show(mAct, "分享取消！", Toast.LENGTH_SHORT);

        }
    };
}
